// pages/order/pay-info/pay-info.js
import {formatTime} from '../../../utils/util'
import { getPayAll } from '../../../network/order';
import { getValetList } from '../../../network/client';
import { toast } from '../../../utils/Tools';
const app = getApp()
Page({
  data: {
    isFirstPay: true,
    fileList: [],
    payDate:null,
    companyArr:[],
    company:null,
    order_id: null,
    customer_id:null,
    pay_info:{},
    picArr:[]
  },
  onLoad: function (options) {
    this.setData({
      payDate:formatTime(new Date()),
      order_id:options.order_id,
      customer_id:options.customer_id
    })
    //      调用网络请求
    this._getValetList()
    this._getPayAll()
  },
   //      事件函数      //
  handleChange(e){ // 处理选择客户数据
    const {type:value} = e.currentTarget.dataset
    this.setData({
      [value]: e.detail.value
    })
  },  
  chooseImage(){
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: res => {
        this.setData({
          fileList:res.tempFilePaths
        })
      }
    })
  },
  viewImage: function (e) {
    //获取当前图片的下标
    var index = e.currentTarget.dataset.index;
    //所有图片
    var pics = this.data.picArr;
    wx.previewImage({
      //当前显示图片
      current: pics[index],
      //所有图片
      urls: pics
    })
  },
  // 预览图片
  previewImg: function (e) {
    //获取当前图片的下标
    var index = e.currentTarget.dataset.index;
    //所有图片
    var pics = this.data.fileList;
    wx.previewImage({
      //当前显示图片
      current: pics[index],
      //所有图片
      urls: pics
    })
  },

  // 删除图片
  deleteImg: function (e) {
    var pics = this.data.fileList;
    var index = e.currentTarget.dataset.index;
    pics.splice(index, 1);
    this.setData({
      fileList: pics,
    })
  },
  formSubmit(e) {
    const {pay_money,pay_date,pay_type} = e.detail.value
    
    for(let i=0;i<this.data.fileList.length;i++){
      wx.uploadFile({
        url: app.globalData.BASE_URL+'api/orders/pay_info',
        filePath: this.data.fileList[i],
        name: `pay_pic`,
        formData: {
          pay_money,
          pay_date,
          pay_company: this.data.companyArr[this.data.company],
          pay_type,
          order_id: this.data.order_id,
          customer_id: this.data.customer_id,
          token: wx.getStorageSync('token')
        }, 
        header: {
          "Content-Type": "multipart/form-data"
        },
        success: res => {
          const data = JSON.parse(res.data)
          if(data.code === 200){
            toast('上传成功','success')
            setTimeout(()=>{
              wx.switchTab({
                url: '/pages/order/order/order',
              })
            },1500)            
          }else{
            toast(data.msg,'loading')
          }
        },
        fail: err =>{
          console.warn(err)
        }
      })
    }
  },
  //      网络请求      //
  async _getValetList(){
    const res = await getValetList()
    this.setData({
      companyArr:res.data.pay_name.name
    })
  },
  async _getPayAll(){
    const res = await getPayAll(this.data.order_id)
    if(res.code === 200){
      this.setData({pay_info: res.data})
      let arr = []
      res.data.forEach(res=>{
        arr.push(res.proof_pic)
      })
      this.setData({
        picArr:arr
      })
    }
  }
})